<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="爬虫编码" prop="spiderCode">
        <el-input
          v-model="queryParams.spiderCode"
          placeholder="请输入爬虫编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="爬虫名称" prop="spiderName">
        <el-input
          v-model="queryParams.spiderName"
          placeholder="请输入爬虫名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="网站根域名" prop="domain">
        <el-input
          v-model="queryParams.domain"
          placeholder="请输入网站根域名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['spider:spiderConfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['spider:spiderConfig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['spider:spiderConfig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['spider:spiderConfig:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="spiderConfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="爬虫编码" align="center" prop="spiderCode" />
      <el-table-column label="爬虫名称" align="center" prop="spiderName" />
      <el-table-column label="网站根域名" align="center" prop="domain" />
      <el-table-column label="字符集" align="center" prop="charset" />
      <el-table-column label="睡眠时间(ms)" align="center" prop="sleepTime" />
      <el-table-column label="重试次数" align="center" prop="retryTimes" />
      <el-table-column label="线程数量" align="center" prop="threadCount" />
      <el-table-column label="使用代理" align="center" prop="useProxy" :formatter="useProxyFormat" />
      <el-table-column label="打印日志" align="center" prop="showLog" :formatter="showLogFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['spider:spiderConfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleField(scope.row)"
            v-hasPermi="['spider:spiderField:add']"
          >配置字段</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['spider:spiderConfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改爬虫配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="爬虫编码" prop="spiderCode">
              <el-input v-model="form.spiderCode" placeholder="请输入爬虫编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="爬虫名称" prop="spiderName">
              <el-input v-model="form.spiderName" placeholder="请输入爬虫名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="入口地址" prop="entryUrls">
              <el-input v-model="form.entryUrls" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="目标URL正则" prop="targetRegex">
              <el-input v-model="form.targetRegex" placeholder="请输入目标URL正则" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="存储的表名" prop="tableName">
              <el-input v-model="form.tableName" placeholder="请输入存储的表名" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="网站根域名" prop="domain">
              <el-input v-model="form.domain" placeholder="请输入网站根域名" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="字符集" prop="charset">
              <el-input v-model="form.charset" placeholder="请输入字符集" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="睡眠时间(ms)" prop="sleepTime">
              <el-input v-model="form.sleepTime" placeholder="请输入睡眠时间(ms)" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="重试次数" prop="retryTimes">
              <el-input v-model="form.retryTimes" placeholder="请输入重试次数" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="线程数量" prop="threadCount">
              <el-input v-model="form.threadCount" placeholder="请输入线程数量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="级联发现url" prop="cascade">
              <el-switch v-model="form.cascade" :active-value="1" :inactive-value="0"   />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="使用代理" prop="useProxy">
              <el-switch v-model="form.useProxy" :active-value="1" :inactive-value="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="打印日志" prop="showLog">
              <el-switch v-model="form.showLog" :active-value="1" :inactive-value="0"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSpiderConfig, getSpiderConfig, delSpiderConfig, addSpiderConfig, updateSpiderConfig, exportSpiderConfig } from "@/api/spider/spiderConfig";

export default {
  name: "SpiderConfig",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 爬虫配置表格数据
      spiderConfigList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 1:级联发现url  0:只从入口页面发现url字典
      cascadeOptions: [],
      // 使用代理字典
      useProxyOptions: [],
      // 打印日志字典
      showLogOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        spiderCode: null,
        spiderName: null,
        domain: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        spiderCode: [
          { required: true, message: "爬虫编码不能为空", trigger: "blur" }
        ],
        spiderName: [
          { required: true, message: "爬虫名称不能为空", trigger: "blur" }
        ],
        targetRegex: [
          { required: true, message: "目标URL正则不能为空", trigger: "blur" }
        ],
        domain: [
          { required: true, message: "网站根域名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_switch_type").then(response => {
      this.cascadeOptions = response.data;
    });
    this.getDicts("sys_switch_type").then(response => {
      this.useProxyOptions = response.data;
    });
    this.getDicts("sys_switch_type").then(response => {
      this.showLogOptions = response.data;
    });
  },
  methods: {
    /** 查询爬虫配置列表 */
    getList() {
      this.loading = true;
      listSpiderConfig(this.queryParams).then(response => {
        this.spiderConfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 1:级联发现url  0:只从入口页面发现url字典翻译
    cascadeFormat(row, column) {
      return this.selectDictLabel(this.cascadeOptions, row.cascade);
    },
    // 使用代理字典翻译
    useProxyFormat(row, column) {
      return this.selectDictLabel(this.useProxyOptions, row.useProxy);
    },
    // 打印日志字典翻译
    showLogFormat(row, column) {
      return this.selectDictLabel(this.showLogOptions, row.showLog);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        spiderCode: null,
        spiderName: null,
        entryUrls: null,
        targetRegex: null,
        cascade: 0,
        tableName: null,
        domain: null,
        charset: null,
        sleepTime: null,
        retryTimes: null,
        threadCount: null,
        useProxy: 0,
        showLog: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加爬虫配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSpiderConfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改爬虫配置";
      });
    },
    /** 配置按钮操作 */
    handleField(row) {
      this.reset();
      const id = row.id || this.ids
      this.$router.push({ path: "/spider/spiderField", query: { configId:id} });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSpiderConfig(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSpiderConfig(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除爬虫配置编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSpiderConfig(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有爬虫配置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSpiderConfig(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
